package org.mock.service.impl;

import com.alibaba.fastjson2.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mock.base.LambdaRequestHandler;
import org.mock.base.RequestExecutor;
import org.mock.dto.*;
import org.mock.entity.*;
import org.mock.service.*;
import org.springframework.stereotype.Service;

/**
 * 用户表 服务实现类
 *
 * @author AI
 * @since 2020-06-19
 */
@Service
@AllArgsConstructor
@Slf4j
public class TravelPortServiceImpl implements TravelPortService {

    private TravelHotelAvailService travelHotelAvailService;
    private TravelHotelDetailService travelHotelDetailService;
    private TravelHotelPriceCheckService travelHotelPriceCheckService;
    private TravelCreatePassengerService travelCreatePassengerService;

    private SabreCreatePassengerService sabreCreatePassengerService;
    private SabreOrderBookingService sabreOrderBookingService;
    private SabreOrderCancelBookingService sabreOrderCancelBookingService;

    @Override
    public String getHotelAvail(JSONObject request) {
        TravelHotelAvailDTO originalQuery = travelHotelAvailService.handleRequest(request);
        TravelHotelAvailDTO queryWrapper = travelHotelAvailService.handleQueryWrapper(originalQuery);
        TravelHotelAvail data = travelHotelAvailService.getOne(queryWrapper);
        return travelHotelAvailService.handleData(data, originalQuery);
    }

    @Override
    public String getHotelDetails(JSONObject request) {
        //解析出初始查询条件
        SabreHotelDetailDTO originalQuery = travelHotelDetailService.handleRequest(request);
        //根据mock场景，加工查询条件
        SabreHotelDetailDTO queryWrapper = travelHotelDetailService.handleQueryWrapper(originalQuery);
        //查询数据
        TravelHotelDetail data = travelHotelDetailService.getOne(queryWrapper);
        //加工数据
        return travelHotelDetailService.handleData(data, originalQuery);
    }

    @Override
    public String priceCheck(JSONObject request) {
        //解析出初始查询条件
        TravelHotelPriceCheckDTO originalQuery = travelHotelPriceCheckService.handleRequest(request);
        //根据mock场景，加工查询条件
        TravelHotelPriceCheckDTO queryWrapper = travelHotelPriceCheckService.handleQueryWrapper(originalQuery);
        //查询数据
        TravelHotelPriceCheck data = travelHotelPriceCheckService.getOne(queryWrapper);
        //加工数据
        return travelHotelPriceCheckService.handleData(data);
    }

    @Override
    public String createPnr(JSONObject request) {
        log.info("[createPnr] 请求：{}", request);
        //第几次调用，就返回第几次的结果
        TravelCreatePassenger one = travelCreatePassengerService.getOne(new TravelCreatePassengerDTO());
        if (null == one) {
            return null;
        }
        return one.getResponseFirst();
    }

    @Override
    public String getBooking(JSONObject request) {
        return RequestExecutor.execute(
                request,
                new LambdaRequestHandler<>(
                        //解析请求体
                        sabreOrderBookingService::handleRequest,
                        //根据配置，修改查询条件
                        sabreOrderBookingService::handleQueryWrapper,
                        //查询
                        sabreOrderBookingService::getOne,
                        //处理返回体
                        sabreOrderBookingService::handleData
                )
        );
    }

    @Override
    public String cancelBooking(JSONObject request) {
        //解析出初始查询条件
        SabreOrderCancelBookingDTO originalQuery = sabreOrderCancelBookingService.handleRequest(request);
        //根据mock场景，加工查询条件
        SabreOrderCancelBookingDTO queryWrapper = sabreOrderCancelBookingService.handleQueryWrapper(originalQuery);
        //查询数据
        SabreOrderCancelBooking data = sabreOrderCancelBookingService.getOne(queryWrapper);
        //加工数据
        return sabreOrderCancelBookingService.handleData(data, originalQuery);
    }
}
